package easycloud.ecs.provider.model.amazon;

import java.util.Date;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.S3ObjectSummary;

import easycloud.ecs.common.domain.Item;
import easycloud.ecs.common.domain.Provider;
import easycloud.ecs.provider.model.IStorageObjectMetadata;

public class AmazonStorageObjectMetadata implements IStorageObjectMetadata {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8965095529504988964L;

	private long contentLength;

	private String hash;

	private Date lastModified;

	private Item item;

	public AmazonStorageObjectMetadata(ObjectMetadata metadata, Item item) {
		contentLength = metadata.getContentLength();
		hash = metadata.getETag();
		lastModified = metadata.getLastModified();
		this.item = item;
	}

	public AmazonStorageObjectMetadata(PutObjectResult putResult, Item item) {
		hash = putResult.getETag();
		this.item = item;
	}

	public AmazonStorageObjectMetadata(S3ObjectSummary summary, Item item) {
		contentLength = summary.getSize();
		hash = summary.getETag();
		lastModified = summary.getLastModified();
		this.item = item;
	}

	@Override
	public String getHash(Provider provider) {
		return hash;
	}

	@Override
	public Item getItem() {
		return item;
	}

	@Override
	public Date getLastModified() {
		return lastModified;
	}

	@Override
	public long getLength() {
		return contentLength;
	}

	@Override
	public String getPath() {
		return item.getPath();
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
